import Util from '../../utils/util.js';
import {getUserInfo} from "../../api/member";

const app = getApp();
Page({
  data: {
    isPhone: false,
    showTipDialog: false,
    iosDialog1: false,
    dialogProfile: false,

    parameter: {
      title: "",
      isBack: true,
    },
    opacity: 0,

    statList: [
      {title: "活动证书", amount: 0,},
      {title: "活动时长", amount: 0,},
      {title: "累计积分", amount: 0,},
    ],

    navList: [
      {title: "个人资料", link: "/pages/member/edit", icon: "profile"},
      {title: "历史活动", link: "/pages/activity/history", icon: "activity"},
      {title: "联系活动顾问", id: "kefu", amount: 0, icon: "kefu"},
      {title: "关于我们", link: "/pages/article/content?id=2", icon: "about_us"},
      {title: "隐私协议", link: "/pages/article/content?id=1", icon: "yinsi",},
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    this.setData({
      bottom: 30,
      navH: app.globalData.navHeight
    })
    Util.isIphone().then(res => {
      this.setData({isIphoneX: res})
    });

    wx.showLoading({
      title: "正在加载..."
    })
    Util.chekWxLogin().then(res => {
      const userInfo = res.userInfo
      that.setData({userInfo: userInfo})

      if (Util.isEmpty(userInfo.username) || Util.isEmpty(userInfo.avatar)) {
        return Util.Tips({title: "请先完善基本资料"}, {tab: 2, url: "../member/edit"})
      }

      getUserInfo().then(res => {
        let statList = that.data.statList;
        statList[2].amount = parseInt(res.data.integral || 0);
        statList[1].amount = parseInt(res.data.activity_time_amount || 0);
        statList[0].amount = parseInt(res.data.activity_cert_amount || 0);
        that.setData({
          statList: statList,
          userInfo: res.data || null
        })
      })

      wx.hideLoading()
    }).catch(errmsg => {
      wx.hideLoading()
    })
  },

  changeSwitch(e) {
    const that = this,
      key = e.currentTarget.dataset.key

    let member = this.data.userInfo
    const v = (member[key] == 1) ? 2 : 1;

    let tips = {
      is_recommend: {content: '退出后附近的人看不到你。退出后若再点附近的人会自动加入。', title: '退出首页推荐'},
    }

    if (v == 2 && tips[key]) {//显示提醒
      Util.showModelTip(tips[key].content, tips[key].title, "确定").then(res => {
        if (res.confirm) {
          that.updateMemberField(key, v)
        } else {
          // member['is_recommend'] = 1;
          that.setData({
            userInfo: member
          })
        }
      })
    } else {// 不显示提醒
      that.updateMemberField(key, v)
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this,
      is_refresh = wx.getStorageSync('is_refresh')

    if (is_refresh) {
      wx.removeStorageSync('is_refresh')
      this.loadUserInfo();
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})